package logic

import (
	"context"
	"shunovel/models/mysql"

	"shunovel/shunovelrpc/internal/svc"
	"shunovel/shunovelrpc/shunovelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookListLogic {
	return &BookListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookListLogic) BookList(in *shunovelrpc.BookListRequest) (*shunovelrpc.BookListResponse, error) {
	// todo: add your logic here and delete this line
	var books []mysql.Book
	mysql.DB.Debug().Find(&books)

	var Booklist []*shunovelrpc.BookList
	for _, book := range books {
		list := shunovelrpc.BookList{
			Title:  book.Title,
			Image:  book.Image,
			Text:   book.Text,
			UserId: int64(book.UserId),
		}
		Booklist = append(Booklist, &list)
	}

	return &shunovelrpc.BookListResponse{
		List: Booklist,
	}, nil
}
